
package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.forms.AddRepOfficeForm;
import com.scalar.chiptrack.users.User;


public class AddRepOfficeAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/addRepOffice.do" ;  // Added by Basha (08/08/2003)

    private ArrayList allrepoffices = new ArrayList();
    private String repvalue = null;

    public ActionForward execute (ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
                                     throws Exception {

        HttpSession session = request.getSession();


             User user = (User)session.getAttribute (SessionKeys.USER_KEY);
             // Added by Basha (08/08/2003)
             if (user == null)
             {
                session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
                return (mapping.findForward ("login"));
             }
            // Added by Basha (08/08/2003)

		HashMap errors = new HashMap();
		request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);

		HashMap info = new HashMap();
		request.setAttribute (SessionKeys.INFO_MESSAGES, info);


		AddRepOfficeForm addrepofficeform=(AddRepOfficeForm) form;
		String action = request.getParameter("action");

		if("insertRepOffice".equalsIgnoreCase(action)) {

			repvalue=addrepofficeform.getNewrepoffice();
			boolean success = false;
			boolean temp = false;


			if((repvalue ==null) || (repvalue.trim().length() == 0)) {
				errors.put ( ERROR_CUSTOMER_INVALID_REP_OFFICE, null );
				return (mapping.findForward ("success"));
			}


			allrepoffices = CustomerDAO.getAllRepOffices();
       		int allrepofficesSize = allrepoffices.size();

			for (int j=0; j<allrepofficesSize; j++) {
				if (repvalue.equalsIgnoreCase((String)allrepoffices.get(j))) {
					temp= true;
				  	break;
				}
			}//for end

			if (temp == true) {
				errors.put ( repvalue +" " + ERROR_CUSTOMER_REP_ALREADY_EXISTS, null );
				return (mapping.findForward ("success"));
			}

			try {
				CustomerDAO.insertRepOffice (null, repvalue);
				info.put ( INFO_REP_OFFICE_SUCCESSFULLY_STORED, null );
				success = true;
			}
			catch (ScalarException se) {
				errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                //errors.put ("Internal Server Error", "Could not cancel packingslip (" + packingSlipForm.getLabel() + ")");
				se.printStackTrace();
			}

	    	return (mapping.findForward ("success"));
		}

        return (mapping.findForward ("success"));
    }

}
